﻿package com.wxsoft.business.sys.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.dao.ISysUnitInfoDao;
import com.wxsoft.business.sys.pojo.SysUnitInfo;
import com.wxsoft.business.sys.service.ISysUnitInfoService;
import com.wxsoft.util.UnitCodeUtil;

@Service("sysUnitInfoService")
public class SysUnitInfoServiceImpl implements ISysUnitInfoService {
	@Resource
	private ISysUnitInfoDao dao;

	public long findCount(SysUnitInfo sysUnitInfo) {
		return dao.findCount(sysUnitInfo);
	}

	public List<SysUnitInfo> findAll(PageHelper page, SysUnitInfo sysUnitInfo) {
		List<SysUnitInfo> r = dao.findAll(page, sysUnitInfo);
		return r;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(SysUnitInfo sysUnitInfo) {

		String unitId = UnitCodeUtil.getCode();
		SysUnitInfo sForQ = new SysUnitInfo();
		sForQ.setUnit_id(unitId);
		List<SysUnitInfo> sysUnitL = dao.getBy(sForQ);
		if (sysUnitL != null && sysUnitL.size() > 0) {
			throw new RuntimeException("单位编号重复，请重新输入！"
					+ sysUnitInfo.getUnit_id() + sysUnitInfo.getFull_name());
		}
		//获取上次序号
		String oldNum = dao.findLastNum();
		int newNum = new BigDecimal(oldNum).intValue()+1;
		
		// 获取单位父类
		this.setForAM(sysUnitInfo);

		sysUnitInfo.setUnit_id(unitId);
		sysUnitInfo.setNum(newNum+"");
		this.dao.insert(sysUnitInfo);
		return "操作成功!";
	}

	public String modify(SysUnitInfo sysUnitInfo) {
		// 获取单位父类
		this.setForAM(sysUnitInfo);

		this.dao.modify(sysUnitInfo);
		return "操作成功!";
	}

	public String examine(SysUnitInfo sysUnitInfo) {

		this.dao.modify(sysUnitInfo);
		return "操作成功!";
	}

	/**
	 * 删除
	 */
	public String delete(SysUnitInfo sysUnitInfo) {
		List<SysUnitInfo> oldUnitInfos = dao.getBy(sysUnitInfo);
		if(oldUnitInfos==null || oldUnitInfos.size()<=0){
			throw new RuntimeException("该单位已经删除，请重新选择！"+sysUnitInfo.getUnit_id());
		}
		SysUnitInfo sysUnitInfo2 = oldUnitInfos.get(0);
		String examine = sysUnitInfo2.getExamine();
		if(examine!=null || !"".equals(examine)){
			throw new RuntimeException("该单位已经审核，不能删除！"+sysUnitInfo.getUnit_id());
		}
		this.dao.delete(sysUnitInfo);
		return "操作成功!";
	}

	/**
	 * 更加条件，查询单位
	 */
	public List<SysUnitInfo> getBy(SysUnitInfo sysUnitInfo) {
		List<SysUnitInfo> r = dao.getBy(sysUnitInfo);
		return r;
	}

	/**
	 * 设置 父菜单 为 增加和修改
	 * 
	 * @param sysMenu
	 */
	public void setForAM(SysUnitInfo sysUnitInfo) {

		if ("0".equals(sysUnitInfo.getHigher_unit_id())) {
			sysUnitInfo.setHigher_unit_ids("[0],");
		} else {
			// 获取父菜单
			SysUnitInfo sForQ = new SysUnitInfo();
			sForQ.setUnit_id(sysUnitInfo.getHigher_unit_id());
			List<SysUnitInfo> sysUnitL = dao.getBy(sForQ);
			if (sysUnitL != null && sysUnitL.size() > 0) {
				for (SysUnitInfo sysU : sysUnitL) {
					// 设置当前父菜单
					sysUnitInfo.setHigher_unit_ids(sysU.getHigher_unit_ids()
							+ "[" + sysUnitInfo.getHigher_unit_id() + "],");
				}
			} else {
				throw new RuntimeException("未找到父单位，请重新选择！");
			}
		}

	}

}
